<?php



class Database{
    
    private $connection;
    private $last_query;
    private $magic_quotes_a;
    private $real_escape_string_e;
    private static $instance;
    
    // Opens database connection
    function __construct() {
        $this->open_conn();
        $this->magic_quotes_a = get_magic_quotes_gpc();
        $this->real_escape_string_e = function_exists("mysql_real_escape_string");
    }
    
    // Returns the instance of Database
    public static function getInstance(){
        if(empty(self::$instance)){
            try{
            self::$instance = new Database();
            } catch (PDOException $e) {
                echo 'Connection failed: ' . $e->getMessage();
            }
        }
        return self::$instance;
}
    
    
    // theese methods are directly linked to our mysql database
    
    public function open_conn(){
        
        
        $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
        
        if(!$this->connection){
            die("Database Connection Failed because: " . mysql_error());
        }else{
            $db_select = mysql_select_db(DB_NAME,$this->connection);
            if(!$db_select){
                die("Database Connection Failed because: " .mysql_error());
            }
        }
    }
    
    // Methods closes databaseconnection
    public function close_conn(){
        if(isset($this->connection)){
            mysql_close($this->connection);
            unset($this->connection);
    }
    }
    
    // Method executes a query to the Database
    public function query($sql){
        $this->last_query = $sql;
        $result = mysql_query($sql, $this->connection);
        $this->confirm_query($result);
        return $result;
    }
    

    // Method removes SQL characters from string, to prevent SQL injection
    public function escape_value($value){
        if($this->real_escape_string_e){
            if($this->magic_quotes_a){
                $value = stripcslashes($value);
            }
            $value = mysql_real_escape_string($value);
        }
        if(!$this->magic_quotes_a){
            $value = addslashes($value);
        }
            return $value;
        }
        
        // this methods(functions) are database neutral and wil work on any type of database
        
        // Returns the count of rows from a result set
      public function num_rows($result_set){
            return mysql_fetch_array($result_set);
        }
        // Returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in resultset. 
      public function fetch_array($result_set){
          return mysql_fetch_array($result_set);
          
      }  
      // Returns the count of affected rows from a query
      public function affected_rows(){
          return mysql_affected_rows($this->connection);
      }
      
      // Returns the ID of the last insert to the Database
      public function insert_id(){
          return mysql_insert_id($this->connection);
      }
      
      // Confirms wether the query is valid or not, returns error if it is not.
      private function confirm_query($result){
          if(!$result){
              $output = "Database failed on query: " . mysql_error()  . "<br />";
              die($output);
          }
      }
    }
    
    $database = new Database();
?>
